package com.echo.im.client.status.listener;

import com.echo.im.client.constant.RedisConstant;
import com.echo.im.client.status.cache.CacheService;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.stereotype.Component;

/**
 *
 * @author echo
 * @date 2024-08-20
 */
public class UserStatusListener implements MessageListener {

    private final CacheService cacheService;

    public UserStatusListener(CacheService cacheService) {
        this.cacheService = cacheService;
    }

    @Override
    public void onMessage(Message message, byte[] pattern) {

        // 假设消息体是用户ID的字符串
        String userIdStr = new String(message.getBody());
        Long userId = Long.parseLong(userIdStr);

        // 删除本地缓存中的该用户的数据
        cacheService.evictCache(RedisConstant.IM_USER_STATUS_CHANNEL, userId);
    }
}
